<?php

function _pr($object, $debug = 1, $printAllBacktrace = 0)
{
	$backtrace = debug_backtrace();
	return my_print_r($object, $debug, $backtrace, 0, $printAllBacktrace);
}

function my_print_r($object, $debug=1, $backtrace, $is_global=0, $printAllBacktrace)
{
		global $global_pr;
		static $debugCounter = 0;

		ob_start();
		print_r($object);
		$textObject = ob_get_contents();
		ob_end_clean();
		$file = isset($backtrace[0]['file']) ? $backtrace[0]['file'] : '';
		$line = isset($backtrace[0]['line']) ? $backtrace[0]['line'] : '';
		$className = isset($backtrace[1]['class']) ? $backtrace[1]['class'] : '';
		$functionName = isset($backtrace[1]['function']) ? $backtrace[1]['function'] : '';
		if($debug) {
			if($is_global) {
					ob_start();
				}
			if($debug === 1) {
				$rows = sizeof(explode("\n",$textObject));
				$rows = $rows > 25 ? 25 : $rows;
				$textObject = htmlspecialchars($textObject);
				$textFieldStyle = "style='width: 100%; border-width:1px; border-color : #000000; background-color : #CCCCCC; font-family: Courier; font-size: 10px'";

				?>
				<div style="border: 1px solid #CCCCCC; margin-bottom: 5px; paddint-top:0px;">
				<table width="100%" cellpadding="0" cellspacing="0">
				<tr><td>
				<? if($rows > 1) { ?>
				<textarea rows="<?=$rows?>" <?=$textFieldStyle?>><?=$textObject?></textarea>
				<? } else { ?>
				<input type="text" value="<?=$textObject?>" <?=$textFieldStyle?> <?=$textFieldStyle?>>
				<? } ?>
				</td></tr>
				<tr><td style="font-size: 10px; font-family: Verdana; width: 100%; margin-top:0px;">
				<? if(!$printAllBacktrace) { ?>
					<?=$file?> <?=$line?>. Class:<?=$className?> , Function: <?=$functionName?>
				<? } else { ?>
					<? foreach ($backtrace as $i=>$oTrace) { foreach (array('file', 'line', 'class', 'function') as $tkey) {$oTrace[$tkey] = isset($oTrace[$tkey]) ? $oTrace[$tkey] : '';} ?>
					#<?$i?>: <?=$oTrace['file']?> <?=$oTrace['line']?> Class: <?=$oTrace['class']?> Function: <?=$oTrace['function']?> <br>

					<? } ?>
				<? } ?>
				</td></tr>
				</table>
				</div>

			<?php
			} elseif($debug === 2) {
				$debugCounter++;
				echo "[[[[[[ Start debug: $debugCounter $line:$file\n$textObject]]]]]] End debug: $debugCounter\n";

			}

			if($is_global) {
				$global_pr .= ob_get_contents();
				ob_end_clean();
			}

		}

	   return $textObject;
}?>